package com.pearlsoft.webapp.rms.vmmgr;

import java.util.List;
import java.util.Map;
import java.util.logging.Level;

import com.pearlsoft.rms.VcloudManager;
import com.pearlsoft.webapp.core.annotation.DataSetHandlerComponent;
import com.pearlsoft.webapp.core.dataset.DataSet;
import com.pearlsoft.webapp.core.dataset.Result;
import com.pearlsoft.webapp.core.web.basehandler.AbsDataSetHandler;
import com.vmware.vcloud.sdk.Organization;
import com.vmware.vcloud.sdk.VM;
import com.vmware.vcloud.sdk.Vapp;
import com.vmware.vcloud.sdk.Vdc;

@DataSetHandlerComponent(name = "VMMgrDatasetHandler")
public class VMMgrDatasetHandler extends AbsDataSetHandler{
	
	/*
	 * 根据VMName从VMlist中返回单个VM对象
	 */
	private VM getVMByName(List<VM> vms,String VMName){
		for(VM v:vms)
		{
			if(v.getResource().getName().equals(VMName))
			{
				return v;
			}
		}
		return null;
	}
	 
	public void handler(DataSet ds, Result result) throws Exception {

	}

	public void save(final DataSet ds, Result result) throws Exception {
		
	}
	
	
	public void delete(final DataSet ds, Result result) throws Exception {
		String vDC_Name = (String)ds.getConditions().get("VDC_Name");
		String vApp_Name = (String)ds.getConditions().get("VApp_Name");
		String vM_Name = (String)ds.getConditions().get("VM_Name");
		VcloudManager vcm = this.getBean("vcloudManager", VcloudManager.class);
		vcm.setLogLevel(Level.OFF);
		//vcm.login("https://192.168.0.7", "administrator@test", "abc123..");
		Map<String, Organization> orgs = vcm.getOrgMap();
		Organization org = orgs.get(System.getProperty("ORG_NAME"));
		Map<String, Vdc> vdcs = vcm.getVdcMap(org);
		Vdc vdc = vdcs.get(vDC_Name);
		Map<String, Vapp> vapps = vcm.getVappMap(vdc);
		Vapp vapp = vapps.get(vApp_Name);
		List<VM> vms = vcm.getVMList(vapp);
		VM vm = getVMByName(vms,vM_Name);
		if(!vm.equals(null)&& vm.getVMStatus().toString().equals("POWERED_OFF"))
		{
		    vm.delete();
		}	
	}
	
	public void deleteAll(final DataSet ds, Result result) throws Exception {
		String vDC_Name = (String)ds.getConditions().get("VDC_Name");
		String vApp_Name = (String)ds.getConditions().get("VApp_Name");
		VcloudManager vcm = this.getBean("vcloudManager", VcloudManager.class);
		vcm.setLogLevel(Level.OFF);
		//vcm.login("https://192.168.0.7", "administrator@test", "abc123..");
		Map<String, Organization> orgs = vcm.getOrgMap();
		Organization org = orgs.get(System.getProperty("ORG_NAME"));
		Map<String, Vdc> vdcs = vcm.getVdcMap(org);
		Vdc vdc = vdcs.get(vDC_Name);
		Map<String, Vapp> vapps = vcm.getVappMap(vdc);
		Vapp vapp = vapps.get(vApp_Name);
		List<VM> vms = vcm.getVMList(vapp);
		for (VM vm:vms)
		{
			vm.delete();
		}	
	}
	/*
	 * 创建虚拟机
	 */
	public void createVM(final DataSet ds, Result result) throws Exception {
		
	}
	
	/*
	 * 修改虚拟机
	 */
	public void editVM(final DataSet ds, Result result) throws Exception {
		
	}
	
	
	
	/*
	 * 启动
	 */
	public void powerOnVM(final DataSet ds, Result result) throws Exception {
		String vM_Name = (String)ds.getConditions().get("VM_Name");
		String vDC_Name = (String)ds.getConditions().get("VDC_Name");
		String vApp_Name = (String)ds.getConditions().get("VApp_Name");
		VcloudManager vcm = this.getBean("vcloudManager", VcloudManager.class);
		vcm.setLogLevel(Level.OFF);
		//vcm.login("https://192.168.0.7", "administrator@test", "abc123..");
		Map<String, Organization> orgs = vcm.getOrgMap();
		Organization org = orgs.get(System.getProperty("ORG_NAME"));
		Map<String, Vdc> vdcs = vcm.getVdcMap(org);
		Vdc vdc = vdcs.get(vDC_Name);
		Map<String, Vapp> vapps = vcm.getVappMap(vdc);
		Vapp vapp = vapps.get(vApp_Name);
		List<VM> vms = vcm.getVMList(vapp);
		VM vm = getVMByName(vms,vM_Name);
		if(!vm.equals(null)&& !vm.getVMStatus().toString().equals("POWERED_ON"))
		{
			vm.powerOn();
		}
	}
	
	/*
	 * 关闭
	 */
	public void shutdownVM(final DataSet ds, Result result) throws Exception {
		String vM_Name = (String)ds.getConditions().get("VM_Name");
		String vDC_Name = (String)ds.getConditions().get("VDC_Name");
		String vApp_Name = (String)ds.getConditions().get("VApp_Name");
		VcloudManager vcm = this.getBean("vcloudManager", VcloudManager.class);
		vcm.setLogLevel(Level.OFF);
		//vcm.login("https://192.168.0.7", "administrator@test", "abc123..");
		Map<String, Organization> orgs = vcm.getOrgMap();
		Organization org = orgs.get(System.getProperty("ORG_NAME"));
		Map<String, Vdc> vdcs = vcm.getVdcMap(org);
		Vdc vdc = vdcs.get(vDC_Name);
		Map<String, Vapp> vapps = vcm.getVappMap(vdc);
		Vapp vapp = vapps.get(vApp_Name);
		List<VM> vms = vcm.getVMList(vapp);
		VM vm = getVMByName(vms,vM_Name);
		if(!vm.equals(null)&& vm.getVMStatus().toString().equals("POWERED_ON"))
		{
			vm.shutdown();
		}
	}
	
	/*
	 * 挂起
	 */
	public void suspendVM(final DataSet ds, Result result) throws Exception{
		String vM_Name = (String)ds.getConditions().get("VM_Name");
		String vDC_Name = (String)ds.getConditions().get("VDC_Name");
		String vApp_Name = (String)ds.getConditions().get("VApp_Name");
		VcloudManager vcm = this.getBean("vcloudManager", VcloudManager.class);
		vcm.setLogLevel(Level.OFF);
		//vcm.login("https://192.168.0.7", "administrator@test", "abc123..");
		Map<String, Organization> orgs = vcm.getOrgMap();
		Organization org = orgs.get(System.getProperty("ORG_NAME"));
		Map<String, Vdc> vdcs = vcm.getVdcMap(org);
		Vdc vdc = vdcs.get(vDC_Name);
		Map<String, Vapp> vapps = vcm.getVappMap(vdc);
		Vapp vapp = vapps.get(vApp_Name);
		List<VM> vms = vcm.getVMList(vapp);
		VM vm = getVMByName(vms,vM_Name);
		if(!vm.equals(null)&& vm.getVMStatus().toString().equals("POWERED_ON"))
		{
			vm.suspend();
		}
	}
	
	/*
	 * 重启
	 */
	public void rebootVM(final DataSet ds, Result result) throws Exception {
		String vM_Name = (String)ds.getConditions().get("VM_Name");
		String vDC_Name = (String)ds.getConditions().get("VDC_Name");
		String vApp_Name = (String)ds.getConditions().get("VApp_Name");
		VcloudManager vcm = this.getBean("vcloudManager", VcloudManager.class);
		vcm.setLogLevel(Level.OFF);
		//vcm.login("https://192.168.0.7", "administrator@test", "abc123..");
		Map<String, Organization> orgs = vcm.getOrgMap();
		Organization org = orgs.get(System.getProperty("ORG_NAME"));
		Map<String, Vdc> vdcs = vcm.getVdcMap(org);
		Vdc vdc = vdcs.get(vDC_Name);
		Map<String, Vapp> vapps = vcm.getVappMap(vdc);
		Vapp vapp = vapps.get(vApp_Name);
		List<VM> vms = vcm.getVMList(vapp);
		VM vm = getVMByName(vms,vM_Name);
		if(!vm.equals(null)&& vm.getVMStatus().toString().equals("POWERED_ON"))
		{
			vm.reboot();
		}
	}
	

}
